#ifndef TOOL_CAMERA_H
#define TOOL_CAMERA_H

#include <QObject>
#include <QCamera>
#include <QCameraInfo>
#include <QList>
#include <QAbstractVideoSurface>
#include <QQuickImageProvider>
#include <QImage>

class VideoframeProvider:public QQuickImageProvider
{
public:
    VideoframeProvider();

    //重载函数：qml通过此函数访问c++获取图像
    QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)override;
    //通过此函数为图像提供者提供图像数据
    static void setImage(const QImage img);

private:


    static QImage ms_image;
};

class Videoframecaputer: public QAbstractVideoSurface
{
    Q_OBJECT
public:
    enum PixelFormat {
        Format_Invalid,
        Format_ARGB32,
        Format_ARGB32_Premultiplied,
        Format_RGB32,
        Format_RGB24,
        Format_RGB565,
        Format_RGB555,
        Format_ARGB8565_Premultiplied,
        Format_BGRA32,
        Format_BGRA32_Premultiplied,
        Format_BGR32,
        Format_BGR24,
        Format_BGR565,
        Format_BGR555,
        Format_BGRA5658_Premultiplied,

        Format_AYUV444,
        Format_AYUV444_Premultiplied,
        Format_YUV444,
        Format_YUV420P,
        Format_YV12,
        Format_UYVY,
        Format_YUYV,
        Format_NV12,
        Format_NV21,
        Format_IMC1,
        Format_IMC2,
        Format_IMC3,
        Format_IMC4,
        Format_Y8,
        Format_Y16,

        Format_Jpeg,

        Format_CameraRaw,
        Format_AdobeDng,

#ifndef Q_QDOC
        NPixelFormats,
#endif
        Format_User = 1000
    };

    Q_ENUM(PixelFormat)

    explicit Videoframecaputer(QObject *parent = nullptr);

    QList<QVideoFrame::PixelFormat> supportedPixelFormats(
            QAbstractVideoBuffer::HandleType handleType = QAbstractVideoBuffer::NoHandle) const;

    bool present(const QVideoFrame &frame) override;

signals:
    void newVideoFrame(QImage img);
};


class Tool_camera : public QObject
{
    Q_OBJECT
public:
    explicit Tool_camera(QObject *parent = nullptr);
    ~Tool_camera();
    void emitsignalnewVideoFrame(QImage img);
    Q_INVOKABLE  void strartCamera(int deviceId);
    Q_INVOKABLE  void closeCamera();

signals:
    void newVideoFrame(QImage img);

private:
    QCamera  *m_camera=nullptr;
    QList<QCameraInfo> info;
    Videoframecaputer  *m_videoframecaputer=nullptr;



};



#endif // TOOL_CAMERA_H
